var payflag = false;
var confirm = false;
$(function () {
    var url = "/except/systemcontroller/getvipmoney.do";
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var type = $("#type").val();
    if(type != "0") {
        $("#buy_btn").hide();
        return;
    }
    var jsonstr = "{token:" + token + "}";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr,
            setid: 1
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message
            if (code == 20000) {
                $("#upgraded").html("现在升级 ¥" + data.data.setvalue);
                $("#paymoney").html("￥:" + data.data.setvalue);
                $("#spare_money_show").html("余额(￥" + data.data.sparemoney + ")");
                var isenough = data.data.isenough;
                if (isenough) {
                    $("#sparemoney").attr("checked", true);
                    $("#wechat").attr("checked", false);
                } else {
                    $("#sparemoney").attr("checked", false).attr("disabled", "disabled");
                    $("#wechat").attr("checked", true);
                }
                remove_advertising();
            } else if (code == 10001 || code == 10002) {
                location.href = "/except/login.jsp";
            } else {
                $("#hintbg").show()
                $("#hint_message").html(message);
            }
        },
        error: function (data) {
            $("#hintbg").show()
            $("#hint_message").html("网络异常");
        }
    });
    $("#confirm_btn").click(function () {
        if(confirm)
            location.href = "/except/files/mine_page.jsp";
    });
});

function pay() {
    var requrl = location.href.split('#')[0];
    $(".cqchoosenum").removeClass("pop_payss");
    $("#background").hide();
    if(payflag)
        return;
    payflag = false;
    var url = "/except/indentcontroller/buyvip.action";
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var jsonstr = "{token:" + token + "}";
    var paytype = $("#paytype :radio:checked").val();

    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr,
            paytype: paytype,
            requrl:requrl
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 20000) {
                confirm = true;
                $("#nonce_str").val(data.data.nonce_str);
                $("#result_sign").val(data.data.result_sign);
                $("#timestamp").val(data.data.indenttime);
                $("#prepay_id").val(data.data.prepay_id);
                $("#payinfo").submit();
                //TODO 判断支付类型，并执行相关操作
                // if(paytype == 1)
                //     location.href = "";
                // else {
                //
                //     //跳转至微信支付
                // }
            } else if (code == 10001 || code == 10002) {
                location.href = "/except/login.jsp";
            } else {
                $("#hintbg").show();
                $("#hint_message").html(message);
            }
        },
        error: function (data) {
            $("#hintbg").show();
            $("#hint_message").html("网络异常");
        }
    });
}